
var data = [{
  id: 1,
  name: "角色名称",
  size: "是",
  date: "02/19/2010",
  children: [{
    id: 2,
    name: "Program Files",
    size: "是",
    date: "03/20/2010",
    children: [{
      id: 21,
      name: "Java",
      size: "是",
      date: "01/13/2010",
      state: "closed",
      children: [{
        id: 211,
        name: "java.exe",
        size: "是",
        date: "01/13/2010"
      }, {
        id: 212,
        name: "jawt.dll",
        size: "否",
        date: "01/13/2010"
      }]
    }]
  }]
}];

function onContextMenu(e, row) {
  if (row) {
    e.preventDefault();
    $(this).treegrid('select', row.id);
    $('#treegrid_menu').menu('show', {
      left: e.pageX,
      top: e.pageY
    });
  }
}

//新增子节点
function addChildNode(item) {
  item = {
    id: 666,
    name: "666"
  };
  var node = $('#treegrid').treegrid('getSelected');
  console.log(node);
  $('#treegrid').treegrid('append', {
    parent: node.id, 
    data: [item]
  });
};

//新增节点
function insertNode(item) {
  item = {
    id: 666,
    name: "666"
  };
  var node = $('#treegrid').treegrid('getSelected');
  if (node) {
    $('#treegrid').treegrid('insert', {
      after: node.id,
      data: item
    });
  }
};

//更新节点
function updateNode(item) {
  item = {
      id: 666,
      name: "666"
  };
  var node = $('#treegrid').treegrid('getSelected');
  $('#treegrid').treegrid('update', {
    id: node.id,
    row: item
  });
};

//更新节点
function removeNode(item) {
  item = {
      id: 666,
      name: "666"
  };
  var node = $('#treegrid').treegrid('getSelected');
  $('#treegrid').treegrid('update', {
    id: node.id,
    row: item
  });
};

//删除节点
function deleteNode() {
  var node = $('#treegrid').treegrid('getSelected');
  console.log(node);
  if (node) {
    $('#treegrid').treegrid('remove', node.id);
  }
};

function popNode() {
    var node = $('#treegrid').treegrid('getSelected');
    if (node) {
      console.log($('#treegrid').treegrid('pop', node.id));
    }
}


$(document).ready(function() {
    $('#treegrid').treegrid("loadData", data);

    $('#role_modal').on('shown.bs.modal', function() {
    $('#tt').tree({
      data: [{
            text: 'Item1',
            state: 'open',
            children: [{
              text: 'Item11'
             },{
              text: 'Item12'
             }]
             },{
             text: 'Item2'
            }]
    });
  });
});